package controllers

import (
	"gitee.com/zhengjianfeng/pay/common"
	"gitee.com/zhengjianfeng/pay/models"
	"gitee.com/zhengjianfeng/pay/services"
	"gitee.com/zhengjianfeng/pay/web/middleware/jwts"
	"github.com/kataras/iris/v12"
	"regexp"
)

type AdminLoginController struct {
	Ctx               iris.Context
	PayAccountService services.PayAccountService
}

// 创建超级管理员账号-请求参数
type PostCreateRequestData struct {
	Nickname  string `json:"nickname"`
	Username  string `json:"username"`
	Password  string `json:"password"`
	AgentRate int    `json:"agent_rate"`
}

// 账号登录
func (c *AdminLoginController) PostAccount() {
	var (
		userInfo    *models.PayAccount
		requestData *PostCreateRequestData
		returnData  common.AccountReturnData
		tokenString string
		err         error
		isOk        bool
	)

	// 获取请求参数
	requestData = &PostCreateRequestData{}
	if err = common.GetRequestParams(c.Ctx, &requestData); err != nil {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, common.PARAMS_PARSE_FAIL.Error(), nil))
		return
	}

	// 验证参数
	if isOk, _ = regexp.MatchString(`^['\w+]{5,15}$`, requestData.Username); !isOk {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, "登录账号必须是5-15位的有效字符串", nil))
		return
	}

	if isOk, _ = regexp.MatchString(`^['\w+]{6,15}$`, requestData.Password); !isOk {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, "登录密码必须是6-15位的有效字符串", nil))
		return
	}

	// 根据登录账号查询用户账号表
	if userInfo = c.PayAccountService.GetOne(requestData.Username); userInfo.Id == 0 {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, "登录账号或密码错误", nil))
		return
	}

	// 检查账号状态
	if userInfo.Status != 1 {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, "该账号已经被关闭，请联系管理员", nil))
		return
	}

	// 验证登录密码
	if userInfo.Password != common.EncryptionForAccount(requestData.Password, userInfo.Username) {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, "登录账号或密码错误r", nil))
		return
	}

	// jwt 获取token
	tokenString = common.GetAuthToken(&jwts.LoginInfo{
		AccountId:  userInfo.Id,
		Nickname:   userInfo.Nickname,
		Username:   userInfo.Username,
		RoleType:   userInfo.RoleType,
		RelevantId: userInfo.RelevantId,
	})

	returnData = common.AccountReturnData{Token: tokenString}

	_, _ = c.Ctx.JSON(common.GetResponseData(1, "success", returnData))
}

// 创建超级管理员账号(内部使用)
func (c *AdminLoginController) PostCreate() {
	var (
		requestData *PostCreateRequestData
		err         error
		isOk        bool
	)

	// 获取请求参数
	requestData = &PostCreateRequestData{}
	if err = common.GetRequestParams(c.Ctx, &requestData); err != nil {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, common.PARAMS_PARSE_FAIL.Error(), nil))
		return
	}

	// 验证参数
	if !(len(requestData.Nickname) > 1) {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, "昵称必须填写", nil))
		return
	}

	if isOk, _ = regexp.MatchString(`^['\w+]{5,15}$`, requestData.Username); !isOk {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, "登录账号必须是5-15位的有效字符串", nil))
		return
	}

	if isOk, _ = regexp.MatchString(`^['\w+]{6,15}$`, requestData.Password); !isOk {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, "登录密码必须是6-15位的有效字符串", nil))
		return
	}

	if _, err = c.PayAccountService.CreateAccount(requestData.Username, requestData.Nickname, requestData.Password, common.PAY_ROLE_TYPE_ADMIN, 0); err != nil {
		_, _ = c.Ctx.JSON(common.GetResponseData(1, err.Error(), nil))
		return
	}

	_, _ = c.Ctx.JSON(common.GetResponseData(0, "success", nil))
	return
}
